package crawler;

import database.ConnectionPool;

import java.sql.*;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

import parser.HtmlParser;


public class WebCrawler {

    private BlockingQueue<String> domQueue;

    private ConnectionPool connectionPool;

    public WebCrawler(){
        initi();
        domQueue = new LinkedBlockingQueue<String>();
    }

    public void loadDOMqueue() throws ClassNotFoundException, SQLException {
        Connection connection = connectionPool.getConnection();
        Statement statement = connection.createStatement();
        ResultSet resultSet;
        resultSet = statement.executeQuery("select startUrl from starturls");
        while(resultSet.next()){
            String url = resultSet.getString(1);
            domQueue.add(url);
        }
    	// domQueue.add("http://www.flanco.ro/foto-video-optica/aparate-foto-d-slr/aparat-foto-dslr-nikon-d3100-obiectiv-18-55-vr.html");
    }

    public void start() throws InterruptedException {
        while(domQueue.size() > 0){
            String url = domQueue.take();
            DomainCrawler domCrawler = new DomainCrawler(url, connectionPool);
            new Thread(domCrawler).start();
        }
    }



    /* setari pentru connection pool:     */
    public void initi() {
        String driver = "org.gjt.mm.mysql.Driver";
        String url = "jdbc:mysql://localhost:3306/tpjad";
        String username = "root";
        String password = "root";

        try {
            connectionPool = new ConnectionPool(driver, url, username, password, initialConnections(), maxConnections(), true);
        } catch(SQLException sqle) {
            System.err.println("Error making pool: " + sqle);
            connectionPool = null;
        }
    }
    protected int initialConnections() {
        return(10);
    }
    protected int maxConnections() {
        return(50);
    }
    public void destroy() {
        connectionPool.closeAllConnections();
    }


}
